package com.liugh.util;

import cn.hutool.core.io.FileUtil;
import com.liugh.base.BusinessException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Fatao on 2018/10/8
 */
public class PDFToImage {
    private static org.slf4j.Logger Logger = LoggerFactory.getLogger(PDFToImage.class);

    // 水印透明度
//    private static float alpha = 0.5f;
    // 水印横向位置
//    private static int positionWidth = 300;
    // 水印纵向位置
//    private static int positionHeight = 300;
    // 水印文字字体
//    private static Font font = new Font("仿宋", Font.BOLD, 200);
    // 水印文字颜色
//    private static Color color = Color.GRAY;


    /**
     * 从本地文件中选择文件
     * 生成pdf的缩略图
     * @param zoom  缩略图显示倍数，1表示不缩放，0.5表示缩小到50%
//     * @param inputFile        需要生成缩略图的书籍的完整路径
//     * @param outputFile    生成缩略图的放置路径
     */
//    public static List<String> pdftoIamge(float zoom,String inputFile, String outputFile) {
//        List<String> list = null;
//        Document document = null;
//        try {
//            document = new Document();
//            document.setFile(inputFile);
//            list = setDocument(document, outputFile, zoom);
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            assert document != null;
//            document.dispose();
//        }
//        return list;
//    }

    //从url中获取文件
    public static List<String> pdfToImgUrl(float zoom, URL url, String outputDir) {
        List<String> list = new LinkedList<>();
        try {
            Document document  = new Document();
            document.setUrl(url);
            list = setDocument(document, outputDir, zoom);
            document.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    private static List<String> setDocument(Document document,String outputFile,float zoom) throws IOException, BusinessException {
        List<String> list = new ArrayList<>();
        float rotation = 0;
        int maxPages = document.getNumberOfPages();

        FileUtil.mkdir(outputFile);
        BufferedImage bfimage;
        for (int i = 0; i < maxPages; i++) {
            bfimage = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, zoom);
            //加水印
//            bfimage = setGraphics(bfimage);
//            RenderedImage rendImage = bfimage;

            try (FileOutputStream outputStream = new FileOutputStream(new File(outputFile + i + ".jpg"));
                 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            ) {
                ImageIO.write(bfimage, "jpg", bufferedOutputStream);
                bfimage.flush();
                list.add(outputFile + i + ".jpg");
                Logger.info("已处理到第 " + i + "张图片");
            }
        }
        return list;
    }


//    public BufferedImage setGraphics(BufferedImage bfimage) {
//        Graphics2D g = bfimage.createGraphics();
//        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
//        // 5、设置水印文字颜色
//        g.setColor(color);
//        // 6、设置水印文字Font
//        g.setFont(font);
//        // 7、设置水印文字透明度
//        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
//        //设置旋转
//        g.rotate(-Math.PI / 6);
//        g.drawString("株洲中车", 0, (bfimage.getHeight() / 2));
//        // 9、释放资源
//        g.dispose();
//        return bfimage;
//    }


}